#Boa:Frame:LedgerFrame

import wx
import wx.grid
import cubudget
from xml.parsers.expat import ExpatError

def create(parent):
    return LedgerFrame(parent)

[wxID_LEDGERFRAME, wxID_LEDGERFRAMEAMOUNT, wxID_LEDGERFRAMECHECKNUM, 
 wxID_LEDGERFRAMECLEAR_BUTTON, wxID_LEDGERFRAMECOMMENTS, wxID_LEDGERFRAMEDATE, 
 wxID_LEDGERFRAMEDELETE_BUTTON, wxID_LEDGERFRAMEDPCDATE, 
 wxID_LEDGERFRAMEENTER_BUTTON, wxID_LEDGERFRAMELISTVIEW1, 
 wxID_LEDGERFRAMESOURCE, wxID_LEDGERFRAMESOURCE, 
 wxID_LEDGERFRAMESPLITTERWINDOW1, wxID_LEDGERFRAMEDESTINATION, 
 wxID_LEDGERFRAMESTATUSBAR1, wxID_LEDGERFRAMETXTAMOUNT, 
 wxID_LEDGERFRAMETXTCHECKNUMBER, wxID_LEDGERFRAMETXTCOMMENTS, 
 wxID_LEDGERFRAMETYPE, wxID_LEDGERFRAMETYPEBOX, 
] = [wx.NewId() for _init_ctrls in range(20)]

[wxID_LEDGERFRAMEMENUFILECLOSE, wxID_LEDGERFRAMEMENUFILEEXIT, 
 wxID_LEDGERFRAMEMENUFILEOPEN, wxID_LEDGERFRAMEMENUFILESAVE, 
 wxID_LEDGERFRAMEMENUFILESAVEAS, 
] = [wx.NewId() for _init_coll_menuFile_Items in range(5)]

[wxID_LEDGERFRAMEMENUTOOLSHEALTH, wxID_LEDGERFRAMEMENUTOOLSINTEREST, 
] = [wx.NewId() for _init_coll_menuTools_Items in range(2)]

[wxID_LEDGERFRAMEMENUHELPABOUT] = [wx.NewId() for _init_coll_menuHelp_Items in range(1)]

class LedgerFrame(wx.Frame):
    
    
    def _init_coll_boxSizer7_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.txtComments, 3, border=2, flag=wx.EXPAND)
        parent.AddSizer(self.boxSizer8, 2, border=0, flag=wx.EXPAND)

    def _init_coll_boxSizer8_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.Clear_button, 1, border=3,
              flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL)
        parent.AddWindow(self.Delete_button, 1, border=3,
              flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL)
        parent.AddWindow(self.Enter_button, 1, border=3,
              flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL)

    def _init_coll_boxSizer6_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.Comments, 0, border=0,
              flag=wx.ALIGN_CENTER_VERTICAL)

    def _init_coll_boxSizer4_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.Date, 1, border=0, flag=wx.ALIGN_CENTER_VERTICAL)
        parent.AddWindow(self.Source, 5, border=0,
              flag=wx.ALIGN_CENTER_VERTICAL)
        parent.AddWindow(self.destination, 0, border=0, flag=wx.ALIGN_CENTER_VERTICAL)

    def _init_coll_boxSizer5_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.dpcDate, 1, border=1,
              flag=wx.ALIGN_CENTER_VERTICAL)
        parent.AddWindow(self.source, 3, border=1,
              flag=wx.ALIGN_CENTER_VERTICAL)
        parent.AddWindow(self.Amount, 1, border=1,
              flag=wx.ALIGN_CENTER_VERTICAL)
        parent.AddWindow(self.txtAmount, 1, border=1,
              flag=wx.ALIGN_CENTER_VERTICAL)

    def _init_coll_boxSizer3_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.Type, 5, border=1, flag=wx.ALIGN_CENTER_VERTICAL)
        parent.AddWindow(self.TypeBox, 1, border=1,
              flag=wx.ALIGN_CENTER_VERTICAL)
        parent.AddWindow(self.CheckNum, 1, border=1,
              flag=wx.ALIGN_CENTER_VERTICAL)
        parent.AddWindow(self.txtCheckNumber, 1, border=1,
              flag=wx.ALIGN_CENTER_VERTICAL)

    def _init_coll_boxSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.listView1, 5, border=0,
              flag=wx.GROW | wx.SHRINK | wx.EXPAND)
        parent.AddWindow(self.splitterWindow1, 0, border=0, flag=wx.EXPAND)
        parent.AddSizer(self.boxSizer2, 2, border=0, flag=wx.SHRINK | wx.EXPAND)

    def _init_coll_boxSizer2_Items(self, parent):
        # generated method, don't edit

        parent.AddSizer(self.boxSizer3, 1, border=0, flag=wx.SHRINK | wx.EXPAND)
        parent.AddSizer(self.boxSizer4, 1, border=0, flag=wx.SHRINK | wx.EXPAND)
        parent.AddSizer(self.boxSizer5, 1, border=0, flag=wx.SHRINK | wx.EXPAND)
        parent.AddSizer(self.boxSizer6, 1, border=0, flag=wx.SHRINK | wx.EXPAND)
        parent.AddSizer(self.boxSizer7, 2, border=2, flag=wx.SHRINK | wx.EXPAND)

    def _init_coll_menuBar1_Menus(self, parent):
        # generated method, don't edit

        parent.Append(menu=self.menuFile, title='File')
        parent.Append(menu=self.menuTools, title=u'Tools')
        parent.Append(menu=self.menuHelp, title='Help')

    def _init_coll_menuHelp_Items(self, parent):
        # generated method, don't edit

        parent.Append(help='Display general information about CU Budget',
              id=wxID_LEDGERFRAMEMENUHELPABOUT, kind=wx.ITEM_NORMAL,
              text='About')
        self.Bind(wx.EVT_MENU, self.OnMenuHelpAboutMenu,
              id=wxID_LEDGERFRAMEMENUHELPABOUT)

    def _init_coll_menuFile_Items(self, parent):
        # generated method, don't edit

        parent.Append(help=u'Open an account file',
              id=wxID_LEDGERFRAMEMENUFILEOPEN, kind=wx.ITEM_NORMAL,
              text='Open')
        parent.Append(help=u'Save the changes made to the open account',
              id=wxID_LEDGERFRAMEMENUFILESAVE, kind=wx.ITEM_NORMAL,
              text='Save')
        parent.Append(help=u'Exports a report of the account',
              id=wxID_LEDGERFRAMEMENUFILESAVEAS, kind=wx.ITEM_NORMAL,
              text=u'Export')
        parent.Append(help=u'Close the current account being worked on',
              id=wxID_LEDGERFRAMEMENUFILECLOSE, kind=wx.ITEM_NORMAL,
              text='Close')
        parent.Append(help=u'Exit the program', id=wxID_LEDGERFRAMEMENUFILEEXIT,
              kind=wx.ITEM_NORMAL, text='Exit')
        self.Bind(wx.EVT_MENU, self.OnMenuFileOpenMenu,
              id=wxID_LEDGERFRAMEMENUFILEOPEN)
        self.Bind(wx.EVT_MENU, self.OnMenuFileSaveMenu,
              id=wxID_LEDGERFRAMEMENUFILESAVE)
        self.Bind(wx.EVT_MENU, self.OnMenuFileSaveasMenu,
              id=wxID_LEDGERFRAMEMENUFILESAVEAS)
        self.Bind(wx.EVT_MENU, self.OnMenuFileCloseMenu,
              id=wxID_LEDGERFRAMEMENUFILECLOSE)
        self.Bind(wx.EVT_MENU, self.OnMenuFileExitMenu,
              id=wxID_LEDGERFRAMEMENUFILEEXIT)

    def _init_coll_menuTools_Items(self, parent):
        # generated method, don't edit

        parent.Append(help='', id=wxID_LEDGERFRAMEMENUTOOLSINTEREST,
              kind=wx.ITEM_NORMAL, text=u'Interest Runner')
        parent.Append(help='', id=wxID_LEDGERFRAMEMENUTOOLSHEALTH,
              kind=wx.ITEM_NORMAL, text=u'Health Check')

    def _init_coll_listView1_Columns(self, parent):
        # generated method, don't edit

        parent.InsertColumn(col=0, format=wx.LIST_FORMAT_LEFT, heading='Date',
              width=95)
        parent.InsertColumn(col=1, format=wx.LIST_FORMAT_LEFT, heading='Source',
              width=100)
        parent.InsertColumn(col=2, format=wx.LIST_FORMAT_LEFT,
              heading=u'Destination', width=100)
        parent.InsertColumn(col=3, format=wx.LIST_FORMAT_LEFT, heading='R',
              width=25)
        parent.InsertColumn(col=4, format=wx.LIST_FORMAT_LEFT, heading='Debit',
              width=130)
        parent.InsertColumn(col=5, format=wx.LIST_FORMAT_LEFT, heading='Credit',
              width=130)
        parent.InsertColumn(col=6, format=wx.LIST_FORMAT_LEFT,
              heading='Balance', width=130)
        parent.InsertColumn(col=7, format=wx.LIST_FORMAT_LEFT,
              heading=u'Comment', width=225)

    def _init_coll_statusBar1_Fields(self, parent):
        # generated method, don't edit
        parent.SetFieldsCount(1)

        parent.SetStatusText(number=0, text='status')

        parent.SetStatusWidths([-1])

    def _init_sizers(self):
        # generated method, don't edit
        self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)

        self.boxSizer2 = wx.BoxSizer(orient=wx.VERTICAL)

        self.boxSizer3 = wx.BoxSizer(orient=wx.HORIZONTAL)

        self.boxSizer4 = wx.BoxSizer(orient=wx.HORIZONTAL)

        self.boxSizer5 = wx.BoxSizer(orient=wx.HORIZONTAL)

        self.boxSizer6 = wx.BoxSizer(orient=wx.HORIZONTAL)

        self.boxSizer7 = wx.BoxSizer(orient=wx.HORIZONTAL)

        self.boxSizer8 = wx.BoxSizer(orient=wx.HORIZONTAL)

        self._init_coll_boxSizer1_Items(self.boxSizer1)
        self._init_coll_boxSizer2_Items(self.boxSizer2)
        self._init_coll_boxSizer3_Items(self.boxSizer3)
        self._init_coll_boxSizer4_Items(self.boxSizer4)
        self._init_coll_boxSizer5_Items(self.boxSizer5)
        self._init_coll_boxSizer6_Items(self.boxSizer6)
        self._init_coll_boxSizer7_Items(self.boxSizer7)
        self._init_coll_boxSizer8_Items(self.boxSizer8)

        self.SetSizer(self.boxSizer1)

    def _init_utils(self):
        # generated method, don't edit
        self.menuFile = wx.Menu(title='File')

        self.menuHelp = wx.Menu(title='Help')

        self.menuBar1 = wx.MenuBar()

        self.menuTools = wx.Menu(title=u'Tools')

        self._init_coll_menuFile_Items(self.menuFile)
        self._init_coll_menuHelp_Items(self.menuHelp)
        self._init_coll_menuBar1_Menus(self.menuBar1)
        self._init_coll_menuTools_Items(self.menuTools)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_LEDGERFRAME, name='LedgerFrame',
              parent=prnt, pos=wx.Point(333, 28), size=wx.Size(937, 663),
              style=wx.DEFAULT_FRAME_STYLE, title=u'CUBudget')
        self._init_utils()
        self.SetClientSize(wx.Size(937, 663))
        self.SetMenuBar(self.menuBar1)
        self.Bind(wx.EVT_ACTIVATE, self.OnLedgerFrameActivate)

        self.statusBar1 = wx.StatusBar(id=wxID_LEDGERFRAMESTATUSBAR1,
              name='statusBar1', parent=self, style=0)
        self._init_coll_statusBar1_Fields(self.statusBar1)
        self.SetStatusBar(self.statusBar1)

        self.Source = wx.StaticText(id=wxID_LEDGERFRAMESOURCE, label='Source',
              name='Source', parent=self, pos=wx.Point(146, 492),
              size=wx.Size(100, 19), style=0)

        self.Date = wx.StaticText(id=wxID_LEDGERFRAMEDATE, label='Date',
              name='Date', parent=self, pos=wx.Point(0, 492), size=wx.Size(156,
              19), style=0)

        self.CheckNum = wx.StaticText(id=wxID_LEDGERFRAMECHECKNUM,
              label='Check  #', name='CheckNum', parent=self, pos=wx.Point(702,
              462), size=wx.Size(117, 19), style=wx.ALIGN_CENTRE)

        self.Amount = wx.StaticText(id=wxID_LEDGERFRAMEAMOUNT,
              label='Amount  $', name='Amount', parent=self, pos=wx.Point(624,
              522), size=wx.Size(156, 19), style=wx.ALIGN_RIGHT)

        self.source = wx.TextCtrl(id=wxID_LEDGERFRAMESOURCE, name='source',
              parent=self, pos=wx.Point(156, 518), size=wx.Size(200, 27),
              style=0, value='')

        self.txtAmount = wx.TextCtrl(id=wxID_LEDGERFRAMETXTAMOUNT,
              name='txtAmount', parent=self, pos=wx.Point(780, 518),
              size=wx.Size(156, 27), style=0, value='0.00')

        self.txtCheckNumber = wx.TextCtrl(id=wxID_LEDGERFRAMETXTCHECKNUMBER,
              name='txtCheckNumber', parent=self, pos=wx.Point(819, 458),
              size=wx.Size(117, 27), style=0, value='')
        self.txtCheckNumber.Enable(False)

        self.Clear_button = wx.Button(id=wxID_LEDGERFRAMECLEAR_BUTTON,
              label='Clear', name='Clear_button', parent=self, pos=wx.Point(562,
              590), size=wx.Size(125, 33), style=wx.RAISED_BORDER)
        self.Clear_button.SetMaxSize(wx.Size(125, 33))
        self.Clear_button.Bind(wx.EVT_BUTTON, self.OnClear_buttonButton,
              id=wxID_LEDGERFRAMECLEAR_BUTTON)

        self.Enter_button = wx.Button(id=wxID_LEDGERFRAMEENTER_BUTTON,
              label='Enter', name='Enter_button', parent=self, pos=wx.Point(810,
              590), size=wx.Size(125, 33), style=wx.RAISED_BORDER)
        self.Enter_button.SetMaxSize(wx.Size(125, 33))
        self.Enter_button.Bind(wx.EVT_BUTTON, self.OnEnter_buttonButton,
              id=wxID_LEDGERFRAMEENTER_BUTTON)

        self.Comments = wx.StaticText(id=wxID_LEDGERFRAMECOMMENTS,
              label='Comments', name='Comments', parent=self, pos=wx.Point(0,
              552), size=wx.Size(76, 19), style=0)

        self.txtComments = wx.TextCtrl(id=wxID_LEDGERFRAMETXTCOMMENTS,
              name='txtComments', parent=self, pos=wx.Point(0, 577),
              size=wx.Size(562, 63), style=0, value='')

        self.dpcDate = wx.DatePickerCtrl(id=wxID_LEDGERFRAMEDPCDATE,
              name='dpcDate', parent=self, pos=wx.Point(0, 518),
              size=wx.Size(156, 27), style=wx.DP_SHOWCENTURY)

        self.listView1 = wx.ListView(id=wxID_LEDGERFRAMELISTVIEW1,
              name='listView1', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(938, 473), style=wx.VSCROLL | wx.LC_REPORT)
        self.listView1.SetLabel('')
        self._init_coll_listView1_Columns(self.listView1)
        self.listView1.Bind(wx.EVT_LIST_ITEM_SELECTED,
              self.OnListView1ListItemSelected, id=wxID_LEDGERFRAMELISTVIEW1)

        self.Delete_button = wx.Button(id=wxID_LEDGERFRAMEDELETE_BUTTON,
              label='Delete', name='Delete_button', parent=self,
              pos=wx.Point(686, 590), size=wx.Size(125, 33), style=0)
        self.Delete_button.SetMaxSize(wx.Size(125, 33))
        self.Delete_button.Bind(wx.EVT_BUTTON, self.OnDeleteButton,
              id=wxID_LEDGERFRAMEDELETE_BUTTON)

        self.TypeBox = wx.Choice(choices=['Deposit', 'Withdrawal', 'Debit',
              'Check'], id=wxID_LEDGERFRAMETYPEBOX, name='TypeBox', parent=self,
              pos=wx.Point(585, 461), size=wx.Size(117, 21), style=0)
        self.TypeBox.SetSelection(0)
        self.TypeBox.Bind(wx.EVT_CHOICE, self.OnTypeBoxChoice,
              id=wxID_LEDGERFRAMETYPEBOX)

        self.Type = wx.StaticText(id=wxID_LEDGERFRAMETYPE,
              label='Transaction Type  ', name='Type', parent=self,
              pos=wx.Point(0, 462), size=wx.Size(586, 19),
              style=wx.ALIGN_RIGHT)

        self.splitterWindow1 = wx.SplitterWindow(id=wxID_LEDGERFRAMESPLITTERWINDOW1,
              name='splitterWindow1', parent=self, pos=wx.Point(0, 457),
              size=wx.Size(937, 0), style=wx.SP_3D)

        self.destination = wx.StaticText(id=wxID_LEDGERFRAMEDESTINATION,
              label='Destination', name='Destination', parent=self,
              pos=wx.Point(300, 492), size=wx.Size(70, 13), style=0)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)

    def promptForSave(self):
        dlg = wx.MessageDialog(self, 'Would you like to save the changes you have made?', 'Save changes?', wx.ICON_QUESTION | wx.YES_NO)
        if(dlg.ShowModal() == wx.ID_YES):
           # Eventually just change to save last used file path, or prompt if new file
           self.OnMenuFileSaveMenu(self)

    def OnMenuFileOpenMenu(self, event):
        # Check if we have unsaved changes
        if(self.core.checkDirty() == 1):
           self.promptForSave()
        dlg = wx.FileDialog(self, wildcard = '*.xml', style=wx.OPEN)
        if dlg.ShowModal():
           path = dlg.GetPath()
           if path != "":
              retCode = self.core.loadFile(path)
              if retCode is IOError:
                 dlg = wx.MessageDialog(None, 'Error opening file', 'Error', wx.OK | wx.ICON_ERROR)
                 dlg.ShowModal();
              elif retCode is ExpatError:
                 dlg = wx.MessageDialog(None, 'Invalid XML File', 'Error', wx.OK | wx.ICON_ERROR)
                 dlg.ShowModal();

    def OnMenuFileSaveMenu(self, event):
        dlg = wx.FileDialog(self, wildcard = '*.xml', style=wx.SAVE)
        if dlg.ShowModal():
           path = dlg.GetPath()
           if path != "":
              retCode = self.core.saveFile(path)
              if retCode is IOError:
                 dlg = wx.MessageDialog(None, 'Error saving file', 'Error', wx.OK | wx.ICON_ERROR)
                 dlg.ShowModal();

    def OnMenuFileSaveasMenu(self, event):
        self.report = features.report.Report()
        self.report.makeReport(self.core.getAllAccounts())

    def OnMenuFileCloseMenu(self, event):
        self.interest.interest_runner(self.core.getAllAccounts())

    def OnMenuFileExitMenu(self, event):
        # Check for changes to DOM
        if(self.core.checkDirty() == 1):
           self.promptForSave()
        exit()

    def OnMenuHelpAboutMenu(self, event):
        wx.MessageBox("See Doc Dir. Good Luck")

    def OnClear_buttonButton(self, event):
        date = self.dpcDate.Label
        self.source.Value = ""
        self.txtComments.Value = ""
        self.txtCheckNumber.Value = ""
        self.txtAmount.Value = "0.00"
        self.intCurrentItem = -1
        self.TypeBox.SetSelection(0)
        self.txtCheckNumber.Enabled = False

    def OnEnter_buttonButton(self, event):
        amt = self.txtAmount.Value
        checkNum = self.txtCheckNumber.Value
        source = self.source.Value

        try:
            amt = float(amt)
        except ValueError:
            wx.MessageBox("Amount is not numeric")
            return
        
        checkTest = str(checkNum.isdigit() or not checkNum)
        if (checkTest == "False"):
            wx.MessageBox("Check# is not numeric")
            return
        
        if (self.source.Value == ""):
            wx.MessageBox("Please input a Source")
            return

        date = wx.DateTime.Format(self.dpcDate.GetValue(), "%m/%d/%Y")

        self.listView1.InsertStringItem(self.intCurrentItemNumber, date)
        self.listView1.SetStringItem(self.intCurrentItemNumber,1, source)
        
        self.intCurrentItemNumber += 1
        
        newTrans = cubudget.Transaction(destinationAccount='', sourceAccount=self.source.Value,amount=amt, date=date, notes=checkNum)
        self.core.addTransaction(newTrans)
        #self.curTrans = cubudget.Transaction(self.)
        #self.core
        
    def OnDeleteButton(self, event):
        try:
           self.listView1.DeleteItem(self.intCurrentItem)
        except:
           return

    def OnListView1ListItemSelected(self, event):
        self.intCurrentItem = self.listView1.GetFocusedItem()
        item = self.listView1.GetItem(self.listView1.GetFocusedItem())
        self.txtComments.Value = self.listView1.GetItemText(self.intCurrentItem)

    def OnLedgerFrameActivate(self, event):
        self.intCurrentItemNumber = 0
        self.core = cubudget.Core()
        self.core.addAccount("Checkbook", 1)
        self.interest = features.interest.Interest_Compund()

    def OnTypeBoxChoice(self, event):
        if (self.TypeBox.Label == "Check"):
            self.txtCheckNumber.Enabled = True
        else:
            self.txtCheckNumber.Enabled = False

